package com.huayue.dao;

import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.huayue.entity.Authority;
import com.huayue.entity.EleTree;
import com.huayue.entity.Role;
import com.huayue.util.PageHelper;
import com.mysql.DBHelper;

/**
 * 权限
 * 
 */
@Component
public class AuthorityDao implements BaseDao<Authority> {
    @Override
    public int insert(Authority entity) {
        return DBHelper.executeUpdate("INSERT INTO authority (name, role_id, menu_id, dep_id) values (?, ?, ?, ?);",
            entity.getName(), entity.getRole_id(), entity.getMenu_id(), entity.getDep_id());
    }

    @Override
    public int update(Authority entity) {
        return DBHelper.executeUpdate("update authority set name=?,role_id=?,menu_id=?,dep_id=? where id=?",
            entity.getName(), entity.getRole_id(), entity.getMenu_id(), entity.getDep_id(), entity.getId());
    }

    @Override
    public int delete(Authority entity) {
        return DBHelper.executeUpdate("update authority set is_deleted=1 where id=?", entity.getId());
    }

    @Override
    public List<Map<String, Object>> findAll(Object... obj) {
        return DBHelper.executeQuery("select * from authority where is_deleted=0");
    }

    @Override
    public List<Map<String, Object>> findPage(PageHelper page, Object... obj) {
        String sql =
            "select a.name parentname,b.* from authority a right JOIN authority b on b.parentid=a.id where 1=1 and b.is_deleted=0 ";
        if (obj[0] != null & "0".equals(obj[0].toString())) {
            sql += " and b.parentid=0";
        } else if (obj[0] != null & "1".equals(obj[0].toString())) {
            sql += " and b.parentid!=0";
        }
        return DBHelper.executeQuery(sql + " limit ?,? ", page.getBegin(), page.getPageSize());
    }

    @Override
    public Map<String, Object> findOne(Authority entity) {
        return DBHelper.executeQuery("select * from authority where 1=1 and is_deleted=0 and id=? ", entity.getId())
            .get(0);
    }

    @Override
    public int count(Object... obj) {
        String sql =
            "select count(*) counts from authority a right JOIN authority b on b.parentid=a.id where 1=1 and b.is_deleted=0 ";
        if (obj[0] != null & "0".equals(obj[0].toString())) {
            sql += " and b.parentid=0";
        } else if (obj[0] != null & "1".equals(obj[0].toString())) {
            sql += " and b.parentid!=0";
        }
        return Integer.valueOf(DBHelper.executeQuery(sql).get(0).get("counts").toString());
    }

    public List<Map<String, Object>> findAllTopAuthority() {
        return DBHelper.executeQuery("select * from authority where is_deleted=0 and parentid=0");
    }

    public int insertBatch(List<EleTree> entity, Object... obj) {
        int successNum = 0;
        if (obj.length == 0) {
            String sql = "delete from authority  WHERE (role_id=(select max(id) from role));";
            successNum += DBHelper.executeUpdate(sql);
            for (EleTree eleTree : entity) {
                successNum += DBHelper.executeUpdate(
                    " INSERT INTO authority ( menu_id,role_id) values (?,(select max(id) from role));",
                    eleTree.getId());
            }
        } else {
            Role role = (Role)obj[0];
            String sql = "delete from authority  WHERE role_id=" + role.getId() + ";";
            successNum += DBHelper.executeUpdate(sql);
            for (EleTree eleTree : entity) {
                successNum += DBHelper.executeUpdate(" INSERT INTO authority ( menu_id,role_id) values (?,?);",
                    eleTree.getId(), role.getId());
            }
        }
        return successNum;
    }
}
